/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */

package com.android.music.view;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.Shader.TileMode;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.android.music.MusicApplication;
import com.android.music.R;
import com.android.music.model.LrcObject;
import com.android.music.util.Util;

public class ViewDeskTopLrc extends View {

    private List<LrcObject> lrc_map;

    private TextPaint paint;

    private int mCurrentPosition;

    private int playingLine;

    private float currentLineWidth;

    private float currentLineWidthii;

    private float playingX = 0;

    private Shader shaderX = null;

    private boolean isFirstLine;

    private int deskSizeOfText;

    public ViewDeskTopLrc(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new TextPaint();
        deskSizeOfText = (int) this.getResources().getDimension(R.dimen.desk_text_size);
        paint.setTextSize(deskSizeOfText);
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setTypeface(Typeface.SANS_SERIF);
        paint.setAntiAlias(true);
        // mMusicApplication = (MusicApplication)
        // context.getApplicationContext();
        lrc_map = MusicApplication.getInstance().getLrcMap();
        isFirstLine = true;
    }

    public void initLrcMap() {
        lrc_map = MusicApplication.getInstance().getLrcMap();
    }

    public void setlrcPosition(int position) {
        if (lrc_map.size() > 1) {
            mCurrentPosition = position + 500;
            findTheLrc();
            this.postInvalidate();
        }
    }

    private int oldLine = 0;

    private void findTheLrc() {
        for (int i = 0; i < lrc_map.size(); i++) {
            LrcObject val = (LrcObject) lrc_map.get(i);
            if (mCurrentPosition < (val.begintime + val.timeline)) {
                playingLine = i;
                break;
            }
        }
        if (lrc_map.size() == 0)
            return;
        if (lrc_map.size() == 1)
            playingLine = 0;

        if (oldLine != playingLine) {
            oldLine = playingLine;
            isFirstLine = !isFirstLine;
        }
        Rect rect = new Rect();
        paint.setTextSize(deskSizeOfText);

        paint.getTextBounds(lrc_map.get(playingLine).lrc, 0, 1, rect);
        currentLineWidth = paint.measureText(lrc_map.get(playingLine).lrc);
        if (playingLine < (lrc_map.size() - 1)) {
            paint.getTextBounds(lrc_map.get(playingLine + 1).lrc, 0, 1, rect);
            currentLineWidthii = paint.measureText(lrc_map.get(playingLine + 1).lrc);
        }
        /** ===确定播放 X 坐标 如果不需要 渐变 ，直接将playingX设为最大值，movingY设为0=== */
        playingX = 4 * (mCurrentPosition - lrc_map.get(playingLine).begintime) * currentLineWidth
                / lrc_map.get(playingLine).timeline;
        shaderX = new LinearGradient(0, 0, playingX, 0, new int[] {
                Util.DESK_TOP_LRC_PLAYING_COLOR, Util.DESK_TOP_LRC_BACKGROUND_COLOR
        }, new float[] {
                (float) 0.5, (float) 0.51
        }, TileMode.CLAMP);
    }

    public void onDraw(Canvas canvas) {

        if (lrc_map.size() == 0)
            return;
        if (playingLine >= lrc_map.size())
            return;
        LrcObject val = (LrcObject) lrc_map.get(playingLine);

        if (isFirstLine) {

            paint.setColor(Util.DESK_TOP_LRC_PLAYING_COLOR);
            paint.setShader(shaderX);
            if (playingX > 4 * 0.75 * getWidth() && currentLineWidth > getWidth()) {
                paint.setTextAlign(Paint.Align.RIGHT);
                canvas.drawText(val.lrc, getWidth(), getHeight() / 2 - 10, paint);
            } else {
                paint.setTextAlign(Paint.Align.LEFT);
                canvas.drawText(val.lrc, 0, getHeight() / 2 - 10, paint);
            }
            paint.setColor(Util.DESK_TOP_LRC_BACKGROUND_COLOR);
            paint.setTextAlign(Paint.Align.RIGHT);
            paint.setShader(null);
            if (playingLine < (lrc_map.size() - 1)) {
                LrcObject valnext = (LrcObject) lrc_map.get(playingLine + 1);
                canvas.drawText(valnext.lrc, currentLineWidthii < getWidth() ? getWidth()
                        : currentLineWidthii, getHeight() / 2 + 30, paint);
            }

        } else {

            paint.setColor(Util.DESK_TOP_LRC_BACKGROUND_COLOR);
            paint.setShader(null);
            paint.setTextAlign(Paint.Align.LEFT);
            if (playingLine < (lrc_map.size() - 1)) {
                LrcObject valnext = (LrcObject) lrc_map.get(playingLine + 1);
                canvas.drawText(valnext.lrc, 0, getHeight() / 2 - 10, paint);
            }
            paint.setColor(Util.DESK_TOP_LRC_PLAYING_COLOR);
            paint.setShader(shaderX);
            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.drawText(val.lrc, currentLineWidthii > getWidth()
                    && playingX < 4 * 0.75 * getWidth() ? currentLineWidthii : getWidth(),
                    getHeight() / 2 + 30, paint);
        }

    }
}
